<?php

namespace common;

use think\facade\Request;
use think\facade\Db;

class ModuleForm
{


    /**
     * input输入框
     * @param array $info 设置的参数数组
     * @param string $value 值
     * @return string 转换后的字符串
     */

    public function text($info, $value)
    {
        extract($info);
        $setup = is_array($setup) ? $setup : string2array($setup);
        isset($setup['ispassword']) && $setup['ispassword'] == 1 ? $inputtext = 'password' : $inputtext = 'text';//如果是密码，则变为密码框
        $action = ACTION;
        $value = $value ? $value : $setup['default'];
        $required == 1 ? $info['required'] = 'required' : '';

        $info['value'] = $value;

        $parseStr = @file_get_contents(config("view.view_path") . "/module_form/text.tpl");
        foreach ($info as $key => $one) {
            $parseStr = str_replace('{{' . $key . '}}', $one, $parseStr);
        }
        return $parseStr;
    }

    /**
     * textarea输入框
     * @param array $info 设置的参数数组
     * @param string $value 值
     * @return string 转换后的字符串
     */

    public function textarea($info, $value)
    {
        extract($info);
        $setup = is_array($setup) ? $setup : string2array($setup);
        isset($setup['ispassword']) && $setup['ispassword'] == 1 ? $inputtext = 'password' : $inputtext = 'text';//如果是密码，则变为密码框
        $action = ACTION;
        if ($action == 'add') {
            $value = $value ? $value : $setup['default'];
        } else {
            $value = $value ? $value : '';
        }
        $info['value'] = $value;
        $required == 1 ? $info['required'] = 'required|' . $pattern : '';
        $parseStr = file_get_contents(config("view.view_path") . "/module_form/textarea.tpl");
        foreach ($info as $key => $one) {
            $parseStr = str_replace('{{' . $key . '}}', $one, $parseStr);
        }
        return $parseStr;
    }

    /**
     * checkbox复选框
     * @param array $info 设置的参数数组
     * @param string $value 值
     * @return string 转换后的字符串
     */
    public function checkbox($info, $value)
    {
        extract($info);
        $setup = is_array($setup) ? $setup : string2array($setup);
        isset($setup['ispassword']) && $setup['ispassword'] == 1 ? $inputtext = 'password' : $inputtext = 'text';//如果是密码，则变为密码框
        $action = ACTION;
        $id = $field;
        if ($action == 'add') {
            $value = $value ? $value : $setup['default'];
        } else {
            $value = $value ? $value : '';
        }
        if (is_array($options)) {
            $optionsarr = $options;
        } else {
            $options = explode("\n", $setup['options']);
            foreach ($options as $r) {
                $v = explode("|", $r);
                $k = trim($v[1]);
                $optionsarr[$k] = $v[0];
            }
        }
        if ($value != '') $value = strpos($value, ',') ? explode(',', $value) : array($value);
        $i = 1;
        $parseStr = '<div class="layui-input-block">';
        foreach ($optionsarr as $key => $r) {
            $key = trim($key);
            $checked = ($value && in_array($key, $value)) ? 'checked' : '';
            $parseStr .= '<input name="' . $field . '[' . $i . ']" id="' . $id . '_' . $i . '" ' . $checked . ' value="' . htmlspecialchars($key) . '" type="checkbox" class="ace" title="' . htmlspecialchars($r) . '">';
            $i++;
        }
        return $parseStr . '</div>';
    }


    /**
     * radio单选框
     * @param array $info 设置的参数数组
     * @param string $value 值
     * @return string 转换后的字符串
     */

    public function radio($info, $value)
    {
        extract($info);
        $setup = is_array($setup) ? $setup : string2array($setup);
        $id = $field;

        $action = ACTION;
        $value = $value ? $value : $setup['default'];

        $parseStr = '<div class="layui-input-inline">';
        $optionsarr = array();
        if (isset($setup['options'])) {
            $options = explode("\n", $setup['options']);
            foreach ($options as $r) {
                $v = explode("|", $r);
                $k = trim($v[1]);
                $optionsarr[$k] = $v[0];
            }
        }
        $i = 1;
        foreach ($optionsarr as $key => $r) {
            $checked = trim($value) == trim($key) ? 'checked' : '';
            if (empty($value) && empty($key)) {
                $checked = 'checked';
            }
            $parseStr .= '<input name="' . $field . '" id="' . $id . '_' . $i . '" ' . $checked . ' value="' . $key . '"  type="radio" class="ace" title="' . $r . '" />';
            $i++;
        }
        return $parseStr . '</div>';
    }


    /**
     * 单图上传框
     * @param array $info 设置的参数数组
     * @param string $value 值
     * @return string 转换后的字符串
     */
    public function image($info, $value)
    {
        extract($info);
        $setup = is_array($setup) ? $setup : string2array($setup);
        $action = ACTION;
        $value = $value ? $value : "";

        $upload_allowext = isset($setup['upload_allowext']) ? $setup['upload_allowext'] : '';
        $info['upload_uri'] = url(SYS_PATH . "/uploads/upimg");
        $info['value'] = $value;
        $info['upload_allowext'] = "jpg,png,gif";
        $required == 1 ? $info['required'] = 'required|' . $pattern : '';
        $parseStr = file_get_contents(config("view.view_path") . "/module_form/image.tpl");
        foreach ($info as $key => $one) {
            $parseStr = str_replace('{{' . $key . '}}', $one, $parseStr);
        }
        return $parseStr;
    }


    /**
     * input输入框
     * @param array $info 设置的参数数组
     * @param string $value 值
     * @return string 转换后的字符串
     */

    public function datetime($info, $value)
    {
        extract($info);
        $setup = is_array($setup) ? $setup : string2array($setup);
        $info['setup'] = is_array($info['setup']) ? $info['setup'] : string2array($info['setup']);
        $action = ACTION;
        $value = $value ? $value : time();
        $value = date('Y-m-d H:i:s', $value);
        $info['value'] = $value;
        $parseStr = file_get_contents(config("view.view_path") . "/module_form/datetime.tpl");
        $required == 1 ? $info['required'] = 'required|' . $pattern : '';
        unset($info['setup']);
        foreach ($info as $key => $one) {
            $parseStr = str_replace('{{' . $key . '}}', $one, $parseStr);
        }

        return $parseStr;
    }

    /**
     * input输入框
     * @param array $info 设置的参数数组
     * @param string $value 值
     * @return string 转换后的字符串
     */

    public function number($info, $value)
    {

        extract($info);
        $setup = is_array($setup) ? $setup : string2array($setup);
        isset($setup['ispassword']) && $setup['ispassword'] == 1 ? $inputtext = 'password' : $inputtext = 'text';//如果是密码，则变为密码框
        $action = ACTION;
        $id = $field;
        $value = $value ? $value : $setup['default'];
        isset($setup['size']) ? $size = $setup['size'] : $size = '';
        $info['inputtext'] = $inputtext;
        $info['value'] = $value;
        unset($info['setup']);
        $required == 1 ? $info['required'] = 'required|' . $pattern : '';

        $parseStr = file_get_contents(config("view.view_path") . "/module_form/number.tpl");
        foreach ($info as $key => $one) {
            $parseStr = str_replace('{{' . $key . '}}', $one, $parseStr);
        }
        return $parseStr;
    }


    /**
     * 富文本编缉器
     * @param array $info 设置的参数数组
     * @param string $value 值
     * @return string 转换后的字符串
     */

    public function editor($info, $value)
    {
        extract($info);
        $setup = is_array($setup) ? $setup : string2array($setup);
        $pattern = ($info['pattern'] != 'defaul') ? '|' . $info['pattern'] : '';
        $action = ACTION;
        $value = $value ? $value : '';
        $info['value'] = $value;
        unset($info['setup']);
        $required == 1 ? $info['required'] = 'required|' . $pattern : '';
        $parseStr = file_get_contents(config("view.view_path") . "/module_form/editor.tpl");
        foreach ($info as $key => $one) {
            $parseStr = str_replace('{{' . $key . '}}', $one, $parseStr);
        }
        return $parseStr;
    }

    /**
     * 省市区联动
     * @param array $info 设置的参数数组
     * @param string $value 值
     * @return string 转换后的字符串
     */
    public function linkage($info)
    {
        extract($info);
        $value = '';
        if ($this->data[$field]) {
            $value = explode(',', $this->data[$field]);
        }
        $region = db::name('region')->where(['pid' => 1])->select();
        $html = '<div class="layui-input-block">';
        $html .= '<select name="' . $field . '[]" id="province" lay-filter="province">';
        $html .= '<option value="">请选择省</option>';
        foreach ($region as $k => $v) {
            if ($value[0] == $v['id']) {
                $html .= '<option selected value="' . $v['id'] . '">' . $v['name'] . '</option>';
            } else {
                $html .= '<option value="' . $v['id'] . '">' . $v['name'] . '</option>';
            }
        }
        $html .= '</select>';
        $html .= '</div>';

        $city = '';
        if ($value[0]) {
            $city = db('region')->where(['pid' => $value[0]])->select();
        }

        $html .= '<div class="layui-input-block">';
        $html .= '<select name="' . $field . '[]" id="city" lay-filter="city">';
        $html .= '<option value="">请选择市</option>';
        if ($city) {
            foreach ($city as $k => $v) {
                if ($value[1] == $v['id']) {
                    $html .= '<option selected value="' . $v['id'] . '">' . $v['name'] . '</option>';
                } else {
                    $html .= '<option value="' . $v['id'] . '">' . $v['name'] . '</option>';
                }
            }
        }

        $html .= '</select>';
        $html .= '</div>';

        $district = '';
        if ($value[1]) {
            $district = db('region')->where(['pid' => $value[1]])->select();
        }


        $html .= '<div class="layui-input-block">';
        $html .= '<select name="' . $field . '[]" id="district" lay-filter="district">';
        $html .= '<option value="">请选择县/区</option>';

        if ($district) {
            foreach ($district as $k => $v) {
                if ($value[2] == $v['id']) {
                    $html .= '<option selected value="' . $v['id'] . '">' . $v['name'] . '</option>';
                } else {
                    $html .= '<option value="' . $v['id'] . '">' . $v['name'] . '</option>';
                }
            }
        }

        $html .= '</select>';
        $html .= '</div>';
        return $html;
    }


}
